package actions;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.SessionAware;
import modelo.Tesina;
import DAO.FactoryDAO;
import DAO.TesinaDAO;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "default")
public class GridTesinas extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 5078264277068533593L;

	private List<Tesina> gridModel;
	private List<Tesina> tesinas;
	private Integer rows = 0;
	private Integer page = 0;
	private Integer total = 0;
	private Integer record = 0;
	private String sord;
	private String sidx;
	private String searchField;
	private String searchString;
	private String searchOper;
	private boolean loadonce = false;
	private static final Log log = LogFactory.getLog(GridTesinas.class);

	private Map<String, Object> session;

	@SuppressWarnings("unchecked")
	@Actions({ @Action(value = "/GridTesinas", results = { @Result(name = "success", type = "json") }) })
	public String execute() {
		log.debug("Page " + getPage() + " Rows " + getRows()
				+ " Sorting Order " + getSord() + " Index Row :" + getSidx());
		log.debug("Search :" + searchField + " " + searchOper + " "
				+ searchString);

		int to = (getRows() * getPage());
		int from = to - getRows();
		TesinaDAO tDAO = FactoryDAO.getTesinaDAO();

		Object list = session.get("mylist");
		if (list != null) {
			tesinas = (List<Tesina>) list;
		} else {
			log.debug("Build new List");
			tesinas = tDAO.listar(from, to, sidx, sord);
		}

		if (getSord() != null && getSord().equalsIgnoreCase("asc")) {
			Collections.sort(tesinas);
		}
		if (getSord() != null && getSord().equalsIgnoreCase("desc")) {
			Collections.sort(tesinas);
			Collections.reverse(tesinas);
		}

		setRecord(tDAO.getTesinaCount(tesinas));

		if (to > getRecord())
			to = getRecord();

		if (loadonce) {
			setGridModel(tesinas);
		} else {

			if (searchField != null) {
				if (searchField.equals("titulo") || searchField.equals("autor")
						|| searchField.equals("director")
						|| searchField.equals("carrera")) {
					if (searchOper.equals("eq"))
						setGridModel(tDAO.buscarTesisEQ(searchField,
								searchString));
					else if (searchOper.equals("ne"))
						setGridModel(tDAO.buscarTesisNE(searchField,
								searchString));
				}
			} else {
				setGridModel(tesinas);
			}

		}
		setTotal((int) Math.ceil((double) getRecord() / (double) getRows()));

		session.put("mylist", tesinas);

		return SUCCESS;

	}

	public String getJSON() {
		return execute();
	}

	/**
	 * @return how many rows we want to have into the grid
	 */
	public Integer getRows() {
		return rows;
	}

	/**
	 * @param rows
	 *            how many rows we want to have into the grid
	 */
	public void setRows(Integer rows) {
		this.rows = rows;
	}

	/**
	 * @return current page of the query
	 */
	public Integer getPage() {
		return page;
	}

	/**
	 * @param page
	 *            current page of the query
	 */
	public void setPage(Integer page) {
		this.page = page;
	}

	/**
	 * @return total pages for the query
	 */
	public Integer getTotal() {
		return total;
	}

	/**
	 * @param total
	 *            total pages for the query
	 */
	public void setTotal(Integer total) {
		this.total = total;
	}

	/**
	 * @return total number of records for the query. e.g. select count(*) from
	 *         table
	 */
	public Integer getRecord() {
		return record;
	}

	/**
	 * @param record
	 *            total number of records for the query. e.g. select count(*)
	 *            from table
	 */
	public void setRecord(Integer record) {

		this.record = record;

		if (this.record > 0 && this.rows > 0) {
			this.total = (int) Math.ceil((double) this.record
					/ (double) this.rows);
		} else {
			this.total = 0;
		}
	}

	/**
	 * @return an collection that contains the actual data
	 */
	public List<Tesina> getGridModel() {
		return gridModel;
	}

	/**
	 * @param gridModel
	 *            an collection that contains the actual data
	 */
	public void setGridModel(List<Tesina> gridModel) {
		this.gridModel = gridModel;
	}

	/**
	 * @return sorting order
	 */
	public String getSord() {
		return sord;
	}

	/**
	 * @param sord
	 *            sorting order
	 */
	public void setSord(String sord) {
		this.sord = sord;
	}

	/**
	 * @return get index row - i.e. user click to sort.
	 */
	public String getSidx() {
		return sidx;
	}

	/**
	 * @param sidx
	 *            get index row - i.e. user click to sort.
	 */
	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	public void setSearchOper(String searchOper) {
		this.searchOper = searchOper;
	}

	public void setLoadonce(boolean loadonce) {
		this.loadonce = loadonce;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

}